
package com.yami.shop.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiniu.common.QiniuException;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;

import com.qiniu.util.Auth;
import com.yami.shop.bean.model.AttachFile;

import com.yami.shop.common.bean.MinioProperties;
import com.yami.shop.common.util.ImgUploadUtil;
import com.yami.shop.common.util.MinioTemplate;
import com.yami.shop.dao.AttachFileMapper;
import com.yami.shop.service.AttachFileService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Objects;

@Service
public class AttachFileServiceImpl extends ServiceImpl<AttachFileMapper, AttachFile> implements AttachFileService {
	@Autowired
	MinioProperties minioProperties ;
	@Autowired
	MinioClient minioClient;
    @Autowired
    private AttachFileMapper attachFileMapper;
	@Autowired
	MinioTemplate minioTemplate;


	@Autowired
	private ImgUploadUtil imgUploadUtil;
    public final static String NORM_MONTH_PATTERN = "yyyy/MM/";

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String uploadFile(MultipartFile file) throws IOException {
		String extName = FileUtil.extName(file.getOriginalFilename());
		String fileName =DateUtil.format(new Date(), NORM_MONTH_PATTERN)+ IdUtil.simpleUUID() + "." + extName;
		AttachFile attachFile = new AttachFile();
		attachFile.setFilePath(fileName);
		attachFile.setFileSize(file.getBytes().length);
		attachFile.setFileType(extName);
		attachFile.setUploadTime(new Date());
		InputStream fileSteam =null;

		if (Objects.equals(imgUploadUtil.getUploadType(), 1)) {
			// 本地文件上传
			attachFileMapper.insert(attachFile);
			return imgUploadUtil.upload(file, fileName);
		} else {

			//上传到minio
				fileSteam = file.getInputStream();
				minioTemplate.putObject(fileSteam,minioProperties.getBucketName(),file.getName(),fileName);
				minioTemplate.getPresignedObjectUrl(minioProperties.getBucketName(),fileName);

				String src=minioTemplate.getPresignedObjectUrl(minioProperties.getBucketName(),fileName);
			    String url ="/"+minioProperties.getBucketName()+"/"+fileName;

			return url;
		}
	}

	@Override
	public void deleteFile(String fileName){
		attachFileMapper.delete(new LambdaQueryWrapper<AttachFile>().eq(AttachFile::getFilePath,fileName));
		try {
			if (Objects.equals(imgUploadUtil.getUploadType(), 1)) {
				imgUploadUtil.delete(fileName);
			} else if (Objects.equals(imgUploadUtil.getUploadType(), 2)) {
				minioTemplate.deleteObject(minioProperties.getBucketName(),fileName);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
